package com.kitty.common.schedule;


import com.kitty.common.start.GameServer;
import com.kitty.common.utils.SpringUtils;
import com.kitty.game.ServerService;
import com.kitty.game.boss.service.NewBossService;
import com.kitty.game.chat.service.ChatService;
import com.kitty.game.equip.service.EquipService;
import com.kitty.game.fight.service.FightService;
import com.kitty.game.player.PlayerService;
import com.kitty.game.player.model.PlayerProfile;
import com.kitty.game.role.model.Account;
import com.kitty.game.role.model.Role;
import com.kitty.game.role.service.AccountService;
import com.kitty.game.role.service.RoleService;
import com.kitty.game.utils.TimeUtil;
import com.kitty.game.welfare.service.WelfareService;
import com.kitty.mina.cache.DataCache;
import lombok.extern.slf4j.Slf4j;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.json.Json;
import org.nutz.lang.util.NutMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component
@Slf4j
public class ScheduledServiceNew {

    @Autowired
    Dao dao;
    @Autowired
    ServerService serverService;
    @Autowired
    EquipService equipService;
    @Autowired
    GameServer gameServer;
    @Autowired
    RoleService roleService;
    @Autowired
    FightService fightService;
    @Autowired
    NewBossService bossService;
    @Autowired
    AccountService accountService;
    @Autowired
    PlayerService playerService;
    @Autowired
    ChatService chatService;



    @Scheduled(cron = "0 0 5 * * ?")
    public void dailyFiveReset() {
        /*刷新道行盒子*/
        ArrayList<Byte> arrayList = new ArrayList<>(DataCache.RECHARGE_REWARD_HISTORYS.keySet());
        for (byte bb: arrayList){
            DataCache.RECHARGE_REWARD_HISTORYS.put(bb, (short) 0);
        }
    }


    @Scheduled(cron = "0 0/59 * * * ?")
    public void randomActivity1() {
        WelfareService welfareService = SpringUtils.getBean(WelfareService.class);
        String content = welfareService.getRandomData().getData();
        NutMap nutMap = Json.fromJson(NutMap.class, content);
        if (nutMap.size() <= 0) {
            return;
        }
        if (nutMap.getInt("end", 0) == 1) {
            return;
        }
        content = nutMap.getString("content");
        /* 随机抽奖=648##3##20:30##21:30*/
        String[] strings = content.split("##");
        String start = TimeUtil.formatDate(new Date());
        String end = start + " " + strings[3];
        start = start + " " + strings[2];
        Date starttime = TimeUtil.parse(start, "yyyy-MM-dd HH:mm");
        Date endtime = TimeUtil.parse(end, "yyyy-MM-dd HH:mm");
        if (new Date().after(endtime)) {
            return;
        }
        /* 开启随机抽奖活动：时间为xx开始-xx结束 凡事充值XX金额.本区随机抽取X名玩家指定奖品.单笔充值XX金额，中奖几率越高.活动结束公布中奖名单*/
        String msg = "开启随机抽奖活动：时间为#R" + start + "开始-" + end + "结束#n，凡事充值#R"+strings[0]+"以上的玩家#n，本区随机抽取#R"
                + strings[1] + "名#n玩家获得指定奖品，单笔充值#R"+strings[0]+"次数越多#n，中奖几率越高，活动结束后公布中奖名单。";
        Account account = dao.fetch(Account.class, Cnd.where("privilege", "=", 200));
        if (account == null){
            account = dao.fetch(Account.class, Cnd.where("privilege", "=", 1000));
        }
        if (account != null) {
            List<PlayerProfile> list = playerService.getPlayersBy(account.getId());
            if (list != null && list.size() > 0) {
                long uid = playerService.getUidBy(list.get(0).getRoleId());
                Role role = playerService.getPlayerBy(uid);
                if (role != null) {
                    chatService.sendLaba(msg, role);
                    return;
                }
            }
        }
        chatService.sendAdnotice(msg);
    }


}
